/**
 * @File SegmentValuesController.java
 * @author Dheeraj Singh
 * @Version 1.0
 * @Date June 22, 2013
 * 
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */

package in.ac.dei.edrp.generalLedger.segmentValues;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Controller;

import flex.messaging.FlexContext;
import flex.messaging.FlexSession;

@Controller
@RemotingDestination("segmentValues")
public class SegmentValuesController {

	@Autowired
	@Qualifier("SegmentValuesDaoImpl")
	private SegmentValuesDao segmentValuesDao;
	
	public List<SegmentValuesBean> getAllApplications(){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		list = segmentValuesDao.getAllApplications(segmentValuesBean);
		return list;
	}
	
	public List<SegmentValuesBean> getAllFlexFields(String applicationId){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		list = segmentValuesDao.getAllFlexFields(segmentValuesBean);
		return list;
	}
	
	public List<SegmentValuesBean> getAllKeyFlexFields(String applicationId, String flexFieldId){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		list = segmentValuesDao.getAllKeyFlexFields(segmentValuesBean);
		return list;
	}
	
	public List<SegmentValuesBean> getSegments(String applicationId, String flexFieldId, String flexFieldCode){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		list = segmentValuesDao.getSegmentDetails(segmentValuesBean);
		return list;
	}
	
	public List<SegmentValuesBean> getSegmentValues(String applicationId, String flexFieldId, String flexFieldCode, int segmentCode){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentCode);
		list = segmentValuesDao.getSegmentValueDetails(segmentValuesBean);
		return list;
	}
	
	public List<SegmentValuesBean> getGroups(String applicationId, String flexFieldId, String flexFieldCode, int segmentCode){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentCode);
		list = segmentValuesDao.getGroupDetails(segmentValuesBean);
		return list;
	}
	
	public String saveEditSegments(List<Map<String, String>> saveRecord, String applicationId, String flexFieldId, String flexFieldCode, int segmentCode){
		String str = "";
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setCreatorId(session.getAttribute("creatorId").toString());
		segmentValuesBean.setModifierId(session.getAttribute("modifierId").toString());
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentCode);
		str = segmentValuesDao.saveEditSegments(saveRecord, segmentValuesBean);
		return str;
	}
	
	public String saveNewSegments(List<Map<String, String>> childRecord, String applicationId, String flexFieldId, String flexFieldCode, int segmentCode){
		String str = "";
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setCreatorId(session.getAttribute("creatorId").toString());
		segmentValuesBean.setModifierId(session.getAttribute("modifierId").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentCode);
		str = segmentValuesDao.saveNewSegments(childRecord, segmentValuesBean);
		return str;
	}
	
	public String deleteSegments(String applicationId, String flexFieldId, String flexFieldCode, int segmentCode, String segmentValue){
		String str = "";
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentCode);
		segmentValuesBean.setSegmentValue(segmentValue);
		str = segmentValuesDao.deleteSegments(segmentValuesBean);
		return str;
	}
	
	public List<SegmentValuesBean> getAccountTypes(){
		FlexSession session = FlexContext.getFlexSession();
		List<SegmentValuesBean> list = null;
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(session.getAttribute("applicationId").toString());
		list = segmentValuesDao.getAccountTypes(segmentValuesBean);
		return list;
	}
	
	
	public List<SegmentValuesBean> getChildRanges(String applicationId, String flexFieldId, String flexFieldCode, int segmentCode, String segmentValue){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentCode);
		segmentValuesBean.setSegmentValue(segmentValue);
		list = segmentValuesDao.getChildDetails(segmentValuesBean);
		return list;
	}
	
	public String setChildRanges(List<Map<String, String>> saveRecord, String applicationId, String flexFieldId, String flexFieldCode, int segmentCode, String segmentValue){
		String str = "";
		
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setCreatorId(session.getAttribute("creatorId").toString());
		segmentValuesBean.setModifierId(session.getAttribute("modifierId").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentCode);
		segmentValuesBean.setSegmentValue(segmentValue);
		str = segmentValuesDao.saveChildRanges(saveRecord, segmentValuesBean);
		return str;
	}
	
	public List<SegmentValuesBean> showHierarchy(String applicationId, String flexFieldId, String flexFieldCode, int segmentId, String segmentValue){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentId);
		segmentValuesBean.setSegmentValue(segmentValue);
		list = segmentValuesDao.viewHierarchy(segmentValuesBean);
		return list;
	}
	
	public List<SegmentValuesBean> showDownHierarchy(String applicationId, String flexFieldId, String flexFieldCode, int segmentId, String segmentValue){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentId);
		segmentValuesBean.setSegmentValue(segmentValue);
		list = segmentValuesDao.showDownHierarchy(segmentValuesBean);
		return list;
	}
	
	public List<SegmentValuesBean> getParentForUpwards(String applicationId, String flexFieldId, String flexFieldCode, int segmentId, String segmentValue){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentId);
		segmentValuesBean.setSegmentValue(segmentValue);
		list = segmentValuesDao.getParentForUpwards(segmentValuesBean);
		return list;
	}
	
	public List<SegmentValuesBean> getValues(String applicationId, String flexFieldId, String flexFieldCode, int segmentId){
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		List<SegmentValuesBean> list = null;
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentId);
		list = segmentValuesDao.getValues(segmentValuesBean);
		return list;
	}
	
	public String moveChildRange(List<Map<String, String>> moveRecord, String applicationId, String flexFieldId, String flexFieldCode, int segmentCode, String sourceValue, String destinationValue){
		String str = "";
		FlexSession session = FlexContext.getFlexSession();
		SegmentValuesBean segmentValuesBean = new SegmentValuesBean();
		segmentValuesBean.setOrganizationId(session.getAttribute("universityCode").toString());
		segmentValuesBean.setCreatorId(session.getAttribute("creatorId").toString());
		segmentValuesBean.setModifierId(session.getAttribute("modifierId").toString());
		segmentValuesBean.setApplicationId(applicationId);
		segmentValuesBean.setFlexFieldId(flexFieldId);
		segmentValuesBean.setFlexFieldCode(flexFieldCode);
		segmentValuesBean.setSegmentId(segmentCode);
		segmentValuesBean.setSourceValue(sourceValue);
		segmentValuesBean.setDestinationValue(destinationValue);
		str = segmentValuesDao.moveChildRange(moveRecord, segmentValuesBean);
		return str;
	}
}
